<?php
	$subdirname = $argv[1];
	$subdir = opendir($subdirname);

	if(file_exists($subdirname."cover.jpg")){ echo '1'; exit; }

	if(file_exists($subdirname."seriestitle.txt")){
		$movTitle = file_get_contents($subdirname."movietitle.txt");
	}else{	
		echo "3"; exit;
	}
			
			$userID = rand(99000000000000,99999999999999);
			$searchURL = "http://www.dvdempire.com/Exec/v5_search_item.asp?userid=".$userID."&site_media_id=&site_id=4&pp=&used=0&search_refined=0&string=";
			$searchpage = file_get_contents($searchURL.urlencode($movTitle));
	
			$itemstartpos = strpos($searchpage, 'item_id=');
			if($itemstartpos){
				$itemendpos = strpos($searchpage, '&', $itemstartpos-1);
				$itempart   = substr($searchpage, $itemstartpos + 8, $itemendpos - $itemstartpos - 8);
				if($itempart != ""){
					$imgurl = "http://images2.dvdempire.com/gen/movies/".$itempart."h.jpg";
					$cover = file_get_contents($imgurl);
					$jpg = fopen($subdirname."cover.jpg","w");
					fwrite($jpg, $cover);
					fclose($jpg);
					echo '1'; exit;
				}
			}
		
	echo "3"; exit;
	// Wegschrijven...
?>
